package com.lww.integral.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lww.integral.service.TabCommodityService;
import com.lww.springcloudapi.entity.TabCommodity;
import com.lww.springcloudapi.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 积分商城 前端控制器
 * </p>
 *
 * @author SHEN
 * @since 2021-03-11
 */
@Api(description = "积分商城")
@RestController
@RequestMapping("/commodity")
public class TabCommodityController {

    @Autowired
    private TabCommodityService tabCommodityService;

    /**
     * 根据商品名字进行模糊查询
     *
     * @param name
     * @return
     */
    @PostMapping("/findQueryByCommodity")
    @ApiOperation(value = "根据商品名字进行模糊查询或者money大于排序 用户调用接口")
//    @PreAuthorize("hasAnyAuthority('admin')")
    public Result findQueryByCommodity(@RequestParam(required = false) String name,
                                       @RequestParam(required = false) Integer money,
                                       @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                       @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Page<TabCommodity> queryByCommodity = tabCommodityService.findQueryByCommodity(name, money, pageNo, pageSize);

        System.out.println("=========>" + queryByCommodity.getRecords().get(0).getImage().toString());

        return new Result().ok(queryByCommodity);
    }


    @PostMapping("/addCommodity")
    @ApiOperation(value = "添加积分商品 管理员操作")
//    @PreAuthorize("hasAnyAuthority('admin')")
    public Result findInsertCommodity(@PathVariable TabCommodity tabCommodity) {
        return Result.ok(tabCommodityService.findInsertCommodity(tabCommodity));
    }


    @PostMapping("/UpdateCommodity")
    @ApiOperation(value = "修改积分商品 管理员操作")
//    @PreAuthorize("hasAnyAuthority('admin')")
    public Result findUpdateCommodity(TabCommodity tabCommodity) {
        return Result.ok(tabCommodityService.findUpdateCommodity(tabCommodity));
    }

    @PostMapping("/DelCommodity/{id}")
    @ApiOperation(value = "删除积分商品 管理员操作")
//    @PreAuthorize("hasAnyAuthority('admin')")
    public Result findDelCommodity(@PathVariable("id") Integer id) {
        boolean delCommodity = tabCommodityService.findDelCommodity(id);
        return Result.ok(delCommodity);
    }

}

